package cn.zhfeat.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import org.apache.commons.lang.time.DateFormatUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @explain
 * @author wschenyongyin
 * @date 2016年4月3日
 */
public class DateUtils {
	public static final long ONE_DAY_LONG = 86400000;
	private static DateUtils classInstance = new DateUtils();
	private static String datePattern = "yyyy-MM-dd";
	private static SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);

	public static DateUtils getInstance() {
		return classInstance;
	}

	/**
	 * Timestamp时间类型转换String Created on 2014-6-6
	 * <p>
	 * Discription:[]
	 * </p>
	 * 
	 * @author:[]
	 * @update:[日期YYYY-MM-DD] [更改人姓名]
	 * @param time
	 * @param pattern
	 * @return String
	 */
	public static String timestamp2string(Timestamp time, String pattern) {
		Date d = new Date(time.getTime());

		if (pattern == null) {
			pattern = "yyyy-MM-dd HH:mm:ss";
		}
		return DateFormatUtils.format(d, pattern);
	}

	/**
	 * Date时间类型转换String Created on 2014-6-6 时间格式yyyy-MM-dd HH:mm
	 * 
	 * @param date
	 * @param pattern
	 * @return String
	 */
	public static String formatDate(Date date, String pattern) {
		if (date == null) {
			date = new Date(System.currentTimeMillis());
		}

		if (pattern == null) {
			pattern = "yyyy-MM-dd HH:mm:ss";
		}
		return DateFormatUtils.format(date, pattern);
	}

	/**
	 * date传null获取当前时间 时间格式yyyy-MM-dd HH:mm Created on 2014-6-6
	 * 
	 * @param date
	 * @return String
	 */
	public static String defaultFormat(Date date) {
		return formatDate(date, null);
	}

	/**
	 * 获取当前时间Date类型 Created on 2014-6-6
	 * 
	 * @return Date
	 */
	public static Date parseDateFormat() {
		SimpleDateFormat fo = new SimpleDateFormat();
		Date date = new Date(System.currentTimeMillis());
		fo.applyPattern("yyyy-MM-dd");

		try {
			date = fo.parse(DateFormatUtils.format(date, "yyyy-MM-dd"));
		} catch (Exception e) {
		}

		return date;
	}

	/**
	 * 根据Timestamp类型返回2014-06-06格式String Created on 2014-6-6
	 * 
	 * @param time
	 * @return String
	 */
	public static String parseTimestampFormat(Timestamp time) {

		if (time != null && !time.equals("")) {

			return parseDateFormat(new Date(time.getTime()));

		} else {

			return "";
		}

	}

	/**
	 * 根据Date转换String格式yyyy-MM-dd Created on 2014-6-6
	 * 
	 * @param date
	 * @return String
	 */
	public static String parseDateFormat(Date date) {
		SimpleDateFormat fo = new SimpleDateFormat();
		fo.applyPattern("yyyy-MM-dd HH:mm");
		String retVal = "0000-00-00 00:00";
		try {
			retVal = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm");
		} catch (Exception e) {
		}

		return retVal;
	}

	/**
	 * 根据String返回Timestamp Created on 2014-6-6
	 * 
	 * @param value
	 * @return Timestamp
	 */
	public static Timestamp formatFromYYYYMMDD(String value) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(value);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		return new Timestamp(date.getTime());
	}

	public static Timestamp formatFromYYYYMMDDhhmmss(String value) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = sdf.parse(value);
		} catch (ParseException e) {
			return null;
		}
		return new Timestamp(date.getTime());
	}

	// public static Date string2Date(String str) {
	// if (StringUtils.isEmpty(str))
	// return new Date();
	// return java.sql.Date.valueOf(str);
	// }

	public static boolean between(Date srcDate, Date startDate, Date endDate) {
		if (startDate.after(srcDate))
			return false;
		if (endDate.before(srcDate))
			return false;
		return true;
	}

	// public static Date getDayStart(Date date) {
	// return string2Date(divideDateForDay(date, "yyyy-MM-dd", 0));
	// // return Timestamp.valueOf(formatDate(date, "yyyy-MM-dd")+" 00:00:00");
	// }
	//
	// /**
	// * 根据传入时间在追加一天
	// * Created on 2014-6-6
	// * @param date
	// * @return
	// */
	// public static Date getDayEnd(Date date) {
	// return string2Date(divideDateForDay(date, "yyyy-MM-dd", 1));
	// // return Timestamp.valueOf(formatDate(date, "yyyy-MM-dd")+" 23:59:59");
	// }

	/**
	 * 给指定时间 追加天数 Created on 2014-6-6
	 * 
	 * @param date
	 * @param pattern
	 *            显示格式
	 * @param num
	 *            需要加的天数
	 * @return
	 */
	public static String divideDateForDay(Date date, String pattern, int num) {
		if (date == null)
			date = new Date(System.currentTimeMillis());
		if (pattern == null)
			pattern = "yyyy-MM-dd HH:mm";
		Calendar cal = null;
		SimpleDateFormat fo = new SimpleDateFormat();
		fo.applyPattern(pattern);
		try {
			fo.format(date);
			cal = fo.getCalendar();
			cal.add(Calendar.DATE, num);
		} catch (Exception e) {
		}
		return fo.format(cal.getTime());
	}

	public static Date divideDateForHour(Date date, String pattern, int num) {
		if (date == null)
			date = new Date(System.currentTimeMillis());
		if (pattern == null)
			pattern = "yyyy-MM-dd HH:mm";
		Calendar cal = null;
		SimpleDateFormat fo = new SimpleDateFormat();
		fo.applyPattern(pattern);
		try {
			fo.format(date);
			cal = fo.getCalendar();
			cal.add(Calendar.HOUR, num);
		} catch (Exception e) {
		}
		return cal.getTime();
	}

	/**
	 * 算出两个时间的相差天数 Created on 2014-6-6
	 * 
	 * @param dateBegin
	 * @param dateEnd
	 * @return
	 */

	public static long subtrationDate(Date dateBegin, Date dateEnd) {

		return (dateBegin.getTime() - dateEnd.getTime()) / (24 * 60 * 60 * 1000);

	}

	/**
	 * 算出两个时间的相差秒数 Created on 2014-6-6
	 * 
	 * @param dateBegin
	 * @param dateEnd
	 * @return
	 */
	public static long subtrationDateBySecond(Date dateBegin, Date dateEnd) {
		return (dateBegin.getTime() - dateEnd.getTime()) / 1000;

	}

	// 当前日期前几天或者后几天的日期
	public static Date afterNDay(int n) {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.DATE, n);
		Date d2 = c.getTime();
		return d2;
	}

	/**
	 * 
	 * Title: afterNDay Description:指定日期加上天数后的日期
	 * 
	 * @param num
	 *            增加的天数
	 * @param date
	 *            指定日期
	 * @return
	 * @throws ParseException
	 */
	public static Date afterNDay(int num, Date date) {

		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.add(Calendar.DATE, num);// num为增加的天数，可以改变的
		date = ca.getTime();
		return date;
	}

	// 当前日期前几秒或者后几秒的时间
	public static Date afterNSecond(int n) {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.SECOND, n);
		Date d2 = c.getTime();
		return d2;
	}

	public static Date afterNMinute(int n) {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.MINUTE, n);
		Date d2 = c.getTime();
		return d2;
	}

	// 当前日期前几天或者后几天的日期
	public static Date afterNDays(Timestamp time, int n) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(time.getTime());
		c.add(Calendar.DATE, n);
		Date d2 = c.getTime();
		return d2;
	}
	public static Date afterNYears(Date date, int n) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(date.getTime());
		c.add(Calendar.YEAR, n);
		Date d2 = c.getTime();
		return d2;
	}
	public static Timestamp transDate(Date date) {
		if (date != null) {
			String dateStr = defaultFormat(date);
			return formatFromYYYYMMDDhhmmss(dateStr);
		}
		return null;
	}

	public static Date transTimestamp(Timestamp time) {
		if (time != null) {
			long t = time.getTime();
			return new Date(t);
		}
		return null;
	}

	// 时间段的第一个时间
	public static Timestamp stringToTime1(String d) {
		Timestamp t = null;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

		Date d1;
		try {
			if (StringUtils.isNotEmpty(d)) {
				d1 = df.parse(d);
				t = new Timestamp(d1.getTime());
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return t;
	}

	// 时间段的第二个时间
	public static Timestamp stringToTime2(String d) {
		Timestamp t = null;
		// StringUtils
		if (StringUtils.isNotEmpty(d)) {
			t = Timestamp.valueOf(d + " 23:59:59");
		}
		return t;
	}

	public static Calendar getYesterDayBegin() {
		Calendar before = Calendar.getInstance();

		before.set(Calendar.DAY_OF_MONTH, before.get(Calendar.DAY_OF_MONTH) - 1);
		before.set(Calendar.HOUR_OF_DAY, 0);
		before.set(Calendar.MINUTE, 0);
		before.set(Calendar.SECOND, 0);
		before.set(Calendar.MILLISECOND, 0);

		return before;
	}

	/**
	 * 查看昨天的日期 还需要调用transCalendarToTimestamp方法 Created on 2014-6-6
	 * 
	 * @return
	 */
	public static Calendar getYesterDayEnd() {
		Calendar after = Calendar.getInstance();
		after.set(Calendar.DAY_OF_MONTH, after.get(Calendar.DAY_OF_MONTH) - 1);
		after.set(Calendar.HOUR_OF_DAY, 23);
		after.set(Calendar.MINUTE, 59);
		after.set(Calendar.SECOND, 59);
		return after;
	}

	/**
	 * Calendar和Timestamp转换
	 * 
	 * @param cal
	 * @return
	 */
	public static Timestamp transCalendarToTimestamp(Calendar cal) {
		Timestamp ts = new Timestamp(cal.getTimeInMillis());
		return ts;
	}

	/**
	 * 根据Timestamp类型参数 返回年后两位月日(例如:140606) Created on 2014-6-6
	 * 
	 * @param time
	 * @return String
	 */
	public static String transTimestamptostr(Timestamp time) {
		if (time != null) {

			Calendar c = Calendar.getInstance();
			c.setTime(time);
			String year = String.valueOf(c.get(c.YEAR));
			String month = String.valueOf(c.get(c.MONTH) + 1);
			String day = String.valueOf(c.get(c.DATE));

			if (month.length() < 2)
				month = "0" + month;
			if (day.length() < 2)
				day = "0" + day;
			return year.substring(2, 4) + month + day;

		}
		return null;
	}

	/**
	 * 根据Calendar日历返回String Created on 2014-6-6
	 * 
	 * @param cal
	 * @return
	 */
	public static String getDataString(Calendar cal) {
		Calendar now = cal;
		String time = now.get(Calendar.YEAR) + "-" + (now.get(Calendar.MONTH) + 1) + "-"
				+ now.get(Calendar.DAY_OF_MONTH) + " " + now.get(Calendar.HOUR_OF_DAY) + ":" + now.get(Calendar.MINUTE)
				+ ":" + now.get(Calendar.SECOND);
		return time;
	}

	public static Calendar parseCalendarDate(String date) {
		Calendar d11 = new GregorianCalendar();
		Date d1 = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");// 时间格式自己设置
		try { // 一定要放到try里面,不然会报错的
			d1 = sdf.parse(date);
		} catch (Exception e) {
		}

		d11.setTime(d1); // OK了,d11就是结果了
		return d11;
	}

	public static Timestamp calendar2Timestamp(Calendar cal) {
		return new Timestamp(cal.getTimeInMillis());
	}

	public static String getDatePath(Calendar cal, String pattern) {
		if (pattern == null) {
			pattern = "yyyy-MM-dd hh:mm:ss";
		}
		SimpleDateFormat sf = new SimpleDateFormat(pattern);
		return sf.format(cal.getTime());
	}

	// Date转化为Calendar
	public static Calendar date2Calendar(Date d) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		return cal;
	}

	/**
	 * 日期比较是否相等
	 * 
	 * @param d1
	 * @param d2
	 * @param type
	 *            比较方式，complete,date,
	 * @return boolean
	 * @author zhou jun
	 */
	public static boolean compere(Date d1, Date d2, String type) {
		if (type.equals("date")) {
			String pattern = "yyyy-MM-dd";
			String date1 = formatDate(d1, pattern);
			String date2 = formatDate(d2, pattern);
			// System.out.println(date1+date2);
			if (date1.equals(date2)) {
				return true;
			}
			return false;
		} else {
			return d1.equals(d2);
		}
	}

	/**
	 * 功能: 将日期对象按照某种格式进行转换，返回转换后的字符串
	 *
	 * @param date
	 *            日期对象
	 * @param pattern
	 *            转换格式 例：yyyy-MM-dd HH:mm:ss
	 * @author yanhechao
	 */
	public static String DateToString(Date date, String pattern) {
		String strDateTime = null;
		SimpleDateFormat formater = new SimpleDateFormat(pattern);
		strDateTime = date == null ? null : formater.format(date);
		return strDateTime;
	}

	/**
	 * 传入Data类型日期，返回字符串类型时间（ISO8601标准时间）
	 * 
	 * @param date
	 * @re
	 */
	public static String getISO8601Timestamp(Date date) {
		TimeZone tz = TimeZone.getTimeZone("UTC");
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
		df.setTimeZone(tz);
		String nowAsISO = df.format(date);
		return nowAsISO;
	}

	public static Date string2Date(String strDate, String pattern) {
		if (StringUtils.isEmpty(pattern))
			pattern = "yy-MM-dd HH:mm:ss";
		SimpleDateFormat formatter = new SimpleDateFormat(pattern);
		Date date;
		try {
			date = formatter.parse(strDate);
		} catch (ParseException e) {
			date = new Date();
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 日期分割（按日）
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("static-access")
	public static List<String> dateSplit(String startDate, String endDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar canlandar1 = Calendar.getInstance();// 开始时间
		Calendar canlandar2 = Calendar.getInstance();// 结束时间
		try {
			canlandar1.setTime(sdf.parse(startDate));
			canlandar2.setTime(sdf.parse(endDate));// 2009-6-5
		} catch (ParseException e) {
			e.printStackTrace();
		}
		List<String> returnList = new ArrayList<String>();
		while (canlandar1.compareTo(canlandar2) < 1) {
			returnList.add(sdf.format(canlandar1.getTime()));
			canlandar1.add(canlandar1.DATE, 1);// 每次循环增加一天
		}
		return returnList;
	}

	/**
	 * 日期分割（按月）
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static List<String> monthSplit(String startDate, String endDate) {
		Calendar canlandar1 = Calendar.getInstance();// 开始时间
		Calendar canlandar2 = Calendar.getInstance();// 结束时间
		canlandar1.setTime(toDate(startDate));// 2009-6-1
		canlandar2.setTime(toDate(endDate));// 2009-6-5
		List<String> returnList = new ArrayList<String>();
		while (canlandar1.compareTo(canlandar2) < 0) {
			String currDate = toDateString(canlandar1.getTime());
			if (StringUtils.isNotBlank(currDate) && currDate.indexOf("-") > 0) {
				returnList.add(currDate.split("-")[0] + "-" + currDate.split("-")[1]);
			}
			canlandar1.add(canlandar1.MONTH, 1);// 每次循环增加一月
		}
		return returnList;
	}

	public static Date toDate(String s) {
		s = StringUtils.trim(s);
		if (s.length() < 1) {
			return null;
		}
		try {
			if (s.length() <= 10) {
				return dateFormat.parse(s);
			}
			return toDate(Timestamp.valueOf(s));
		} catch (Exception e) {
		}
		return null;
	}

	public static Date toDate(Timestamp timestamp) {
		if (timestamp == null) {
			return null;
		}
		return new Date(timestamp.getTime());
	}

	public static String toDateString(Date date) {
		if (date == null) {
			return "";
		}
		return dateFormat.format(date);
	}

	/**
	 * 获取上个月的第一天
	 * 
	 * @param date
	 * @return
	 */
	public static String precedingMonthStart(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		String firstDay = null;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		// 获取前月的第一天
		calendar.add(Calendar.MONTH, -1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为前月第一天
		firstDay = format.format(calendar.getTime());
		return firstDay;
	}

	/**
	 * 获取上个月的最后一天
	 * 
	 * @param date
	 * @return
	 */
	public static String precedingMonthEnd(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		String firstDay = null;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		// 获取前月的最后一天
		calendar.set(Calendar.DAY_OF_MONTH, 0);
		firstDay = format.format(calendar.getTime());
		return firstDay;
	}

	/**
	 * 获取本月的第一天
	 * 
	 * @param date
	 * @return
	 */
	public static String nowMonthStart(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		String firstDay = null;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		// 获取本月的第一天
		calendar.add(Calendar.MONTH, 0);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		firstDay = format.format(calendar.getTime());
		return firstDay;
	}

	/**
	 * 获取本月的最后一天
	 * 
	 * @param date
	 * @return
	 */
	public static String nowMonthEnd(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		String firstDay = null;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		// 获取前月的最后一天
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		firstDay = format.format(calendar.getTime());
		return firstDay;
	}

	// 获取传入时间天的开始时间
	public static String getDayStartTime(String day) {
		Date date = string2Date(day, "yyyy-MM-dd");
		Calendar todayStart = Calendar.getInstance();
		todayStart.setTime(date);
		todayStart.set(Calendar.HOUR_OF_DAY, 0);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		return formatDate(todayStart.getTime(), null);
	}

	public static Date getDayStartTimeDate(String day) {
		Date date = string2Date(day, "yyyy-MM-dd");
		Calendar todayStart = Calendar.getInstance();
		todayStart.setTime(date);
		todayStart.set(Calendar.HOUR, 0);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		return todayStart.getTime();
	}

	// 获取传入时间天的结束时间
	public static String getDayEndTime(String day) {
		Date date = string2Date(day, "yyyy-MM-dd");
		Calendar todayEnd = Calendar.getInstance();
		todayEnd.setTime(date);
		todayEnd.set(Calendar.HOUR_OF_DAY, 23);
		todayEnd.set(Calendar.MINUTE, 59);
		todayEnd.set(Calendar.SECOND, 59);
		return formatDate(todayEnd.getTime(), null);
	}

	// 当前时间的后n天
	public static Date getDayEndTime(int n) {
		Calendar todayEnd = Calendar.getInstance();
		todayEnd.setTime(new Date());
		todayEnd.set(Calendar.HOUR_OF_DAY, 0);
		todayEnd.set(Calendar.MINUTE, 0);
		todayEnd.set(Calendar.SECOND, 0);
		todayEnd.set(Calendar.MILLISECOND, 0);
		todayEnd.add(Calendar.DATE, n);
		return todayEnd.getTime();
	}

	public static Date getDayEndTimeDate(String day) {
		Date date = string2Date(day, "yyyy-MM-dd");
		Calendar todayEnd = Calendar.getInstance();
		todayEnd.setTime(date);
		todayEnd.set(Calendar.HOUR_OF_DAY, 23);
		todayEnd.set(Calendar.MINUTE, 59);
		todayEnd.set(Calendar.SECOND, 59);
		return todayEnd.getTime();
	}

	public static Date getDayStartTimeDate() {
		Calendar todayStart = Calendar.getInstance();
		todayStart.set(Calendar.HOUR_OF_DAY, 0);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		return todayStart.getTime();
	}

	public static Date getDayStartTimeDate(Date date) {
		Calendar todayStart = Calendar.getInstance();
		todayStart.setTime(date);
		todayStart.set(Calendar.HOUR_OF_DAY, 0);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		return todayStart.getTime();
	}

	public static Date getDayEndTimeDate(Date date) {
		Calendar todayEnd = Calendar.getInstance();
		todayEnd.setTime(date);
		todayEnd.set(Calendar.HOUR_OF_DAY, 23);
		todayEnd.set(Calendar.MINUTE, 59);
		todayEnd.set(Calendar.SECOND, 59);
		return todayEnd.getTime();
	}

	public static Date getDayEndTimeDate() {
		Calendar todayEnd = Calendar.getInstance();
		todayEnd.set(Calendar.HOUR_OF_DAY, 23);
		todayEnd.set(Calendar.MINUTE, 59);
		todayEnd.set(Calendar.SECOND, 59);
		return todayEnd.getTime();
	}

	/**
	 * 获取
	 * 
	 * @param date
	 * @return majie
	 */
	public static String getCron(Date date) {
		String CRON_DATE_FORMAT = "ss mm HH dd MM ? yyyy";
		SimpleDateFormat sdf = new SimpleDateFormat(CRON_DATE_FORMAT);
		String formatTimeStr = "";
		if (date != null) {
			formatTimeStr = sdf.format(date);
		}
		return formatTimeStr;
	}

	/**
	 * 切割时间段
	 *
	 * @param dateType
	 *            交易类型 M/D/H/N -->每月/每天/每小时/每分钟
	 * @param start
	 *            yyyy-MM-dd HH:mm:ss
	 * @param end
	 *            yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static List<String> cutDate(String dateType, String start, String end) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date dBegin = sdf.parse(start);
			Date dEnd = sdf.parse(end);
			return findDates(dateType, dBegin, dEnd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List<String> findDates(String dateType, Date dBegin, Date dEnd) {
		List<String> listDate = new ArrayList<>();
		Calendar calBegin = Calendar.getInstance();
		calBegin.setTime(dBegin);
		Calendar calEnd = Calendar.getInstance();
		calEnd.setTime(dEnd);
		String dateFormat = null;
		while (calEnd.after(calBegin)) {
			switch (dateType) {
			case "M":
				calBegin.add(Calendar.MONTH, 1);
				dateFormat = "MM";
				break;
			case "D":
				calBegin.add(Calendar.DAY_OF_YEAR, 1);
				dateFormat = "YYYY-MM-dd";
				break;
			case "H":
				calBegin.add(Calendar.HOUR, 1);
				dateFormat = "HH";
				break;
			case "W":
				calBegin.add(Calendar.DAY_OF_YEAR, 1);
				dateFormat = "E";
				break;
			case "N":
				calBegin.add(Calendar.SECOND, 1);
				dateFormat = "yyyy-MM-dd HH:mm:ss";
				break;
			default:
				dateFormat = "yyyy-MM-dd HH:mm:ss";
				break;
			}
			if (calEnd.after(calBegin)) {
				if (dateType.equals("W")) {
					listDate.add(new SimpleDateFormat(dateFormat, Locale.CHINA).format(calBegin.getTime()));
				} else {
					listDate.add(new SimpleDateFormat(dateFormat).format(calBegin.getTime()));
				}

			} else {
				if (dateType.equals("W")) {
					listDate.add(new SimpleDateFormat(dateFormat, Locale.CHINA).format(calEnd.getTime()));
				} else {
					listDate.add(new SimpleDateFormat(dateFormat).format(calEnd.getTime()));
				}

			}

		}
		return listDate;
	}

	public static void main(String[] args) throws ParseException {
		// DecimalFormat df = new DecimalFormat("0.0000");
		// System.out.println(df.format((double) 1/ 6));
		// double aa=Double.parseDouble((df.format((double) 1/ 6)));
		// System.out.println(aa);
		// aa=aa*100;
		// System.out.println(aa);
		//
		// DecimalFormat df2=new DecimalFormat("0.00");
		// double d=((double) 1/ 13)*100;
		// d=Double.valueOf(df2.format(d));
		// System.out.println(d);'
		// for (int i = 0; i < 30; i++) {
		// Calendar calendar = Calendar.getInstance();
		// System.out.println(calendar.getTime().getTime());
		// calendar.add(Calendar.DAY_OF_MONTH, 1);
		// System.out.println("-->" + calendar.getTime().getTime());
		// System.out.println("----->" + System.nanoTime());
		// }
		//
		// long timerNano = System.nanoTime();
		// String couponNumber = String.valueOf(timerNano);
		// couponNumber = couponNumber.substring(0, 12);
		// System.out.println("couponNumber---->" + couponNumber);
		// List<String> returnList = dateSplit("2017-10-01","2017-10-17");
		// System.out.println(returnList);
		// System.out.println(afterNDay(-3, new Date()));
		// System.out.println("上月第一天"+precedingMonthStart(new Date()));
		// System.out.println("上月最后一天"+precedingMonthEnd(new Date()));
		/**
		 * rule
		 */
		JSONObject resultJson = new JSONObject();
		/**
		 * 分组选人
		 */
		JSONArray memberArray = new JSONArray();
		JSONObject memberGroup = new JSONObject();
		// 分组id
		memberGroup.put("id", 1);
		memberArray.add(memberGroup);
		memberGroup = new JSONObject();
		memberGroup.put("id", 2);
		memberArray.add(memberGroup);
		resultJson.put("memberGroup", memberArray);
		System.out.println(resultJson);
		/**
		 * 条件选人
		 */
		// 基本信息
		JSONObject customer = new JSONObject();
		// m-男 f-女 u-未知
		customer.put("gender", "m");
		customer.put("startAge", "20");
		customer.put("endAge", "50");
		// all-所有 include-包括 remove-排除
		customer.put("cityType", "remove");
		JSONArray cityArray = new JSONArray();
		JSONObject city = new JSONObject();
		city.put("city", "上海");
		city.put("city", "北京");
		cityArray.add(city);
		customer.put("cityInfo", cityArray);
		// 教育水平
		customer.put("startEdu", "专科");
		customer.put("endEdu", null);
		// 会员等级
		customer.put("startLevel", null);
		customer.put("endLevel", "5");
		resultJson.put("customer", customer);
		System.out.println(resultJson.toJSONString());
		// rfm
		JSONObject rfm = new JSONObject();
		// 前置pre 后置pos 关系relation
		JSONObject pre = new JSONObject();
		pre.put("action", "BUY_PRICE");
		pre.put("dateType", "BEFORE");
		pre.put("dateInfo", "5");
		pre.put("status", "MAXPRICE");
		pre.put("start", "5000");
		pre.put("end", "10000");
		rfm.put("pre", pre);
		// 关系 and--并且 not --不是 or --或者
		rfm.put("relation", "AND");
		JSONObject pos = new JSONObject();
		pos.put("action", "BUY_PRICE");
		// pre.put("dateType","BEFORE");
		// pre.put("dateInfo","5");
		pos.put("status", "BUY_NUMBER");
		pos.put("start", "5");
		pos.put("end", "10");
		rfm.put("pos", pos);
		System.out.println(rfm);
		/**
		 * 触发条件选人
		 */
		JSONObject tigger = new JSONObject();
		// CREATE_CUSTOMER 创建会员 BUY_GOODS 购买商品 BUY_PRICE 购买金额 BIRTHDAY 生日 BALANCE 消费金额
		tigger.put("type", "BUY_GOODS");
		tigger.put("info", "阿莫西林");
		resultJson.put("tigger", tigger);
		/**
		 * 行为选人
		 */
		JSONObject action = new JSONObject();
		// CHANGE -转化 NO_CANCEL -未核销 CANCEL -核销
		action.put("type", "CHANGE");
		resultJson.put("action", action);
		System.out.println(resultJson);
		/**
		 * date
		 */
		JSONObject dateJson = new JSONObject();
		// now -立即执行 -custom 自定义 -after 之后
		dateJson.put("type", "now");
		dateJson.put("info", "0");
		System.out.println(dateJson);
		/**
		 * 动作
		 */
		JSONObject actionJson = new JSONObject();
		// sms -短信 coupon -优惠券
		actionJson.put("type", "sms");
		actionJson.put("info", "1");
		System.out.println(actionJson);
	}
}
